<template>
  <el-container>
    <!--头部-->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="layout">退出</el-button>
    </el-header>
    <el-container>
      <!--左侧-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <div class="menuCollapse" @click="changeCollapse">|||</div>
        <!--左侧菜单栏-->
        <el-menu
          :default-active="$route.path"
          router
          :collapse="isCollapse"
          :collapse-transition="false"
          unique-opened
          class="el-menu-vertical-demo"
          background-color="#373d41"
          text-color="#fff"
          active-text-color="#4091ff">
          <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
            <!--一级菜单-->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!--二级菜单-->
            <el-menu-item-group v-for="subItem in item.children" :key="subItem.id">
              <el-menu-item :index="'/'+subItem.path">
                <i class="el-icon-menu"></i>{{subItem.authName}}
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!--右侧-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data () {
    return {
      menuList: [],
      iconsObj: {
        125: 'iconfont icon-icon_user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false
    }
  },
  created () {
    this.getMenuList()
  },
  methods: {
    // 退出
    layout: function () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取左侧菜单数据
    getMenuList: async function () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status !== 200) return this.$message.error('获取菜单列表失败！')
      this.menuList = res.data
    },
    // 切换隐藏左侧栏
    changeCollapse: function () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
  .el-container{
    height: 100%;
  }
  /*头部*/
  .el-header{
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: #fff;
    font-size: 20px;
    div{
      display: flex;
      align-items: center;
      span{
        margin-left: 15px;
      }
    }
  }
  /*左侧*/
  .el-aside{
    background-color: #373d41;
    .menuCollapse{
      background-color: #4a5064;
      text-align: center;
      color: #fff;
      letter-spacing: 0.2em;
      font-size: 14px;
      line-height: 26px;
      height: 26px;
    }
    .el-menu{
      border-right: 0;
    }
  }
  /*右侧*/
  .el-main{
    background-color: #eaedf1;
  }
  /*字体图标*/
  .iconfont{
    margin-right: 10px;
  }
</style>
